/* myserver.cc: sample server program */

#include "../server.h"
#include "../connection.h"
#include "../connectionclosedexception.h"
#include "messagehandler.h"

#include <iostream>
#include <string>
#include <cstdlib>

using namespace std;
using client_server::Server;
using client_server::Connection;
using client_server::ConnectionClosedException;
//using client_server::MessageHandler;


string readString(Connection& conn) {
    string s;
    char ch;
    while ((ch = conn.read()) != '$')  // '$' is end of string
        s += ch;
    return s;
}

/*
 * Send the string 's' to a client.
 */
void writeString(const string& s, Connection* conn) {
    for (size_t i = 0; i < s.size(); ++i)
        conn->write(s[i]);
    conn->write('$');   // used to indicate end of the string
}
//DEPRICATED
int main(int argc, char* argv[]){
    if (argc != 2) {
        cerr << "Usage: myserver port-number" << endl;
        exit(1);
    }
    
    Server server(atoi(argv[1]));
    if (! server.isReady()) {
        cerr << "Server initialization error" << endl;
        exit(1);
    }
    
    while (true) {
        Connection* conn = server.waitForActivity();
        if (conn != 0) {
            try {
//                MessageHandler mh(conn);
		if(conn.getParameters() == protocol::Protocol::COM_LIST){
		}else if(conn.getParameters() == protocol::Protocol::COM_CREATE){
		}else if(conn.getParameters() == protocol::Protocol::COM_DELETE){
		}else if(conn.getParameters() == protocol::Protocol::COM_GET_ART){
		}
		
            }
            catch (ConnectionClosedException&) {
                server.deregisterConnection(conn);
                delete conn;
                cout << "Client closed connection" << endl;
            }
        }
        else {
            server.registerConnection(new Connection);
            cout << "New client connects" << endl;
        }
    }
}
